package weaver.jobs.zkwz.duss;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-12-06
 * @Description:试用期考核分为两个阶段，一个是指标确认阶段，第二个是考评阶段，第一阶段是员工入职之后根据入职时间触发，
 * 第二阶段根据第一阶段填的截止时间提前 10 个工作日去触发给考核人的直接上级
 * @ModifyLog:
 **/

public class WeSyqkhpfbCronJob extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        try {
            String currentDate = DateUtil.getCurrentDate();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            baseBean.writeLog(">>>>>>WeSyqkhpfbCronJob>>>>currentDate" + currentDate);
            String sql = "select requestid,sqr,sqrbm,gh,zw,zzrq,syqsj from formtable_main_45 where sfgd=0 and ifnull(sfycfpfb,1) !=0";
            rs.executeQuery(sql);
            while (rs.next()) {
                Map<String, String> requestMainMap = new HashMap<>();
                String requestid = Util.null2String(rs.getString("requestid"));
                String sqr = Util.null2String(rs.getString("sqr"));
                String sqrbm = Util.null2String(rs.getString("sqrbm"));
                String gh = Util.null2String(rs.getString("gh"));
                String zw = Util.null2String(rs.getString("zw"));
                String zzrq = Util.null2String(rs.getString("zzrq"));
                String syqsj = Util.null2String(rs.getString("syqsj"));
                String tenDate = DateUtil.addDate(zzrq, -15);
                int compDay = DateUtil.compDate(tenDate, currentDate);
                baseBean.writeLog(">>>>>>WeSyqkhpfbCronJob>>>>compDay" + compDay);
                if (compDay== 0) {
                    String requestName = "试用期考核评分表-" + resourceComInfo.getLastname(sqr) + "-" + currentDate;
                    requestMainMap.put("sqr", sqr);
                    requestMainMap.put("gh", gh);
                    requestMainMap.put("sqrbm", sqrbm);
                    requestMainMap.put("sqrq", currentDate);
                    requestMainMap.put("zw", zw);
                    requestMainMap.put("syqsj", syqsj);
                    requestMainMap.put("syqkhzbs", requestid);
                    boolean status = WorkflowCreate(sqr, requestName, requestMainMap);
                    if (status) {
                        rs.executeUpdate("update formtable_main_45 set sfycfpfb=0 where requestid=?", requestid);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 触发流程
     */
    public static boolean WorkflowCreate(String id, String requestName, Map<String, String> requestMainMap) {
        String workflowid =  getWorkflowId("-33");
        String newrequestid = WorkflowCreateByRequestMap(id, workflowid, requestName,"0", requestMainMap);
        return Util.getIntValue(newrequestid) > 0;
    }
    /**
     * 获取生效版本的workflowID
     *
     * @param formid
     * @return
     */
    public static String getWorkflowId(String formid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select ifnull(activeVersionID,id) from workflow_base where formid=? group by ifnull(activeVersionID,id)", formid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * @param @param  creatorId    创建人Id
     * @param @param  workflowId   流程Id
     * @param @param  requestName  请求标题
     * @param @param  IsNextFlow   是否提交到下一节点
     * @param @param  requestMap   Map格式的主表数据<fieldname,fieldvalue>=<字段名称,字段值>
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: WorkflowCreateByMainTableMap
     */
    public static String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("-----创建流程传递的参数个数--------");
        baseBean.writeLog("creatorId:" + creatorId);
        baseBean.writeLog("workflowId:" + workflowId);
        baseBean.writeLog("requestName:" + requestName);
        baseBean.writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);
        requestInfo.setWorkflowid(workflowId);
        requestInfo.setDescription(requestName);
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            baseBean.writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        baseBean.writeLog("-----创建流程传递的参数个数p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);
            } catch (Exception e) {
                baseBean.writeLog("=======" + e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            baseBean.writeLog(sbf.toString());
            System.out.println("" + sbf.toString());
        } catch (Exception e) {
            baseBean.writeLog("错误：" + e);
            System.out.println("错误：" + e);
        }
        return requestid;
    }

}

